package com.zzstm.eduorder.service.impl;

import com.zzstm.commonUtils.CourseWebVOComment;
import com.zzstm.commonUtils.UcenterMemberCommon;
import com.zzstm.eduorder.client.EduClient;
import com.zzstm.eduorder.client.UCenterClient;
import com.zzstm.eduorder.entity.Order;
import com.zzstm.eduorder.mapper.OrderMapper;
import com.zzstm.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzstm.eduorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-20
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {


    @Qualifier("com.zzstm.eduorder.client.UCenterClient")
    @Autowired
    private UCenterClient uCenterClient;

    @Autowired
    private EduClient eduClient;

    //生成订单
    @Override
    public String createOrder(String courseId, String userId) {
        //获取用户信息
        UcenterMemberCommon user = uCenterClient.getUserInfoById(userId);
        //获取课程信息
        CourseWebVOComment course = eduClient.getCourseInfoById(courseId);

        if (course == null){
            return null;
        }
        //填充order数据
        //创建Order对象，向order对象里面设置需要数据
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());//订单号
        order.setCourseId(courseId); //课程id
        order.setCourseTitle(course.getTitle());
        order.setCourseCover(course.getCover());
        order.setTeacherName(course.getTeacherName());
        order.setTotalFee(course.getPrice());
        order.setMemberId(userId);
        order.setMobile(user.getMobile());
        order.setNickname(user.getNickname());
        order.setStatus(0);  //订单状态（0：未支付 1：已支付）
        order.setPayType(1);  //支付类型 ，微信1
        baseMapper.insert(order);
        return order.getOrderNo();
    }
}
